package com.hangman;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class LanguagePicker extends Activity {

	private String chosenLanguage = "";
	private SoundPlayer sounds;
	private ImageButton englishButton, norwegianButton, soundButton;
	private boolean soundState = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//Exit if requested by another activity
		if (getIntent().getBooleanExtra("doExit", false)) {
			super.finish();
		}
		
		setContentView(R.layout.main);

		sounds = new SoundPlayer(getBaseContext());

		englishButton = (ImageButton) findViewById(R.id.englishButton);
		norwegianButton = (ImageButton) findViewById(R.id.norwegianButton);
		soundButton = (ImageButton) findViewById(R.id.soundButton);

		englishButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				chosenLanguage = "English";
				goToGameOptions();
			}

		});

		norwegianButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				chosenLanguage = "Norwegian";
				goToGameOptions();
			}
		});

		soundButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (soundState) {
					soundState = false;
					soundButton.setImageResource(R.drawable.sound_off);
				} else {
					soundState = true;
					soundButton.setImageResource(R.drawable.sound_on);
				}
			}
		});
	}

	private void goToGameOptions() {
		if (soundState)
			sounds.bubblepop();
		Intent gameOptions = new Intent("com.hangman.GAMEOPTIONS");
		gameOptions.putExtra("chosenLanguage", chosenLanguage);
		gameOptions.putExtra("soundState", soundState);
		startActivity(gameOptions);
	}

	public boolean onCreateOptionsMenu(Menu meny) {
		super.onCreateOptionsMenu(meny);
		meny.add("Exit");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getTitle().equals("Exit")) {
			super.finish();
		}
		return true;
	}
}
